package edu.je.history.Service;

import com.junqson.je.common.UserHolder;
import com.junqson.je.common.model.ApiResult;
import com.junqson.je.common.model.User;
import edu.je.history.Mapper.HistoryInfoMapper;
import edu.je.history.Mapper.JobInfoMapper;
import edu.je.history.Mapper.StationInfoMapper;
import edu.je.history.Model.HistoryInfo;
import edu.je.history.Model.JobInfo;
import edu.je.history.Vo.HistoryResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description:
 * @author: Anhlaidh
 * @date: 2021-04-21 16:16
 */
@Service
public class HistoryService {

    static final int DEFAULT_COUNTS = 10;

    @Autowired
    HistoryInfoMapper historyInfoMapper;
    @Autowired
    StationInfoMapper stationInfoMapper;
    @Autowired
    JobInfoMapper jobInfoMapper;

    public ApiResult PutBrowseHistory(int companyId) {
        User user = UserHolder.get();
        HistoryInfo info = historyInfoMapper.getByIdAndCompany(user.getUserId(), companyId);
        //是否存在浏览记录
        if (info == null) {
            info = new HistoryInfo();
            info.setUserid(user.getUserId());
            info.setCompanyid(companyId);
            info.setViewTime(System.currentTimeMillis());
            historyInfoMapper.insert(info);
        } else {

            info.setViewTime(System.currentTimeMillis());
            historyInfoMapper.update(info);
        }
        return ApiResult.success();
    }

    public ApiResult GetBrowseHistory() {
        User user = UserHolder.get();
        List<HistoryInfo> list = historyInfoMapper.getRecentlyHistory(user.getUserId(), DEFAULT_COUNTS);
        int[] ids = new int[list.size()];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = list.get(i).getCompanyid();
        }
        List<JobInfo> jobInfos = jobInfoMapper.selectByPrimaryKeySet(ids);
        List<HistoryResultVo> result = new ArrayList<>();
        for (int i = 0; i < jobInfos.size(); i++) {
            JobInfo info = jobInfos.get(i);
            HistoryResultVo cur = new HistoryResultVo();
            cur.setName(info.getCompany());
            cur.setSalary(info.getBenefits());
            cur.setTitle(info.getTitle());
            cur.setCompanyId(info.getJobId());
            cur.setTags(new String[]{});
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getCompanyid() == cur.getCompanyId()) {
                    if (list.get(j).getIsmarked()) {
                        cur.setTags(new String[]{"已收藏"});
                    }
                }
            }
            result.add(cur);
        }
        return ApiResult.success(result);
    }

    public ApiResult DelBrowseHistory(int companyId) {
        User user = UserHolder.get();
        HistoryInfo historyInfo = historyInfoMapper.getByIdAndCompany(user.getUserId(),companyId);
        if (historyInfo == null) {
            return ApiResult.error("no data in SQL");
        }
        int code = historyInfoMapper.deleteByIdAndCompany(user.getUserId(), companyId);
        return ApiResult.success(code);
    }
}
